package com.homestay.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.homestay.beans.UserBean;
import com.homestay.dao.IUserDao;
import com.homestay.service.IUserService;

@Service("userService") // IUserService service = new UserServiceImpl()
public class UserServiceImpl implements IUserService {

	// 通过类型自动注入
	@Autowired
	private IUserDao userDao;

	/**
	 * 重写接口登录方法
	 */
	@Override
	public String userLogin(String username, String password) {
		UserBean user = userDao.findUserByUserName(username);
		if(null == user){
			return "该用户不存在";
		}
		else {
			if(password.equals(user.getU_pass())){
				return "密码正确";
			}
			else {
				return "密码不正确";
			}
		}
	}
	
	/**
	 * 新用户注册，加入事务
	 */
	@Override
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED)
	public String userRegister(UserBean user) {
		UserBean existUser = userDao.findUserByUserName(user.getU_account());
		if(null != existUser){
			return "用户已存在!";
		}
		int result1 = userDao.addNewUser(user);
//		int a = 1/0;
		int result2 = userDao.addNewUserInfo(user.getUserInfo());
		if ((result1&result2)==1){
			return "注册成功!";
		}else{
			return "注册失败!";
		}
	}
}
